package baseFx.tests.springboot;

import baseFx.common.AutoCloseable;
import baseFx.common.utils.StringUtils;
import baseFx.springboot.ActionErrorEventArgs;
import baseFx.springboot.ActionEventArgs;
import baseFx.springboot.FxController;
import baseFx.springboot.context.WebApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController extends FxController implements AutoCloseable {
    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private ApplicationContext context;

    public TestController() {
        actionBefore.add(this::onBefore);
        actionAfter.add(this::onAfter);
        actionError.add(this::onError);
    }

    private void onBefore(Object target, ActionEventArgs e) {
        System.out.println("action before " + e.request.getRequestURL());
    }

    private void onAfter(Object target, ActionEventArgs e) {
        System.out.println("action after " + e.request.getRequestURL());
    }

    private void onError(Object target, ActionErrorEventArgs e) {
        System.out.println("action error " + e.error.getMessage());
        e.result = e.error.getMessage();
    }

    @GetMapping("/")
    public Object index() {
        return this.toString();
    }

    @GetMapping("/writeLog")
    public void writeLog() {
        logger.error("error:{}", logger.isErrorEnabled());
        logger.warn("warn:{}", logger.isWarnEnabled());
        logger.info("info:{}", logger.isInfoEnabled());
        logger.debug("debug:{}", logger.isDebugEnabled());
        logger.trace("trace:{}", logger.isTraceEnabled());
    }

    @GetMapping("/page")
    public Object page() {
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("id", 1);
        mv.addObject("name", "zhangsan");
        return mv;
    }

    @GetMapping("/throwEx")
    public void throwEx() {
        throw new RuntimeException("发生异常");
    }

    @Autowired
    private TestRequestAuth auth;

    @GetMapping("/login")
    public void login(HttpServletRequest request) {
        String name = getStringParam("name").get();
        auth.login(request, name);
    }

    @RequestMapping("/showBody")
    public void showBody() {
        logger.info(getRequestBody());
        logger.info(StringUtils.join(",", getParamKeys()));
    }

    @GetMapping("setlog")
    public Object setlog() {
        Logger logger = LoggerFactory.getLogger("org.springframework.beans.factory.support.DisposableBeanAdapter");
        return logger.isDebugEnabled();
    }

    @Override
    public void close() {

    }
}
